/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Codigo;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author kyerte
 */
public class FrmCitas extends javax.swing.JFrame {

    private ConsultaCita consultaCita;
    private List<Cita> resultadoCitas;
    private Cita citaActual;
    private int numeroCitas;
    private int indiceActual;
    private int idCita;
    public static String clienteObtenido;
    private boolean nuevaCita;
    int idCliente = 0;
    int idPromotor = 0;

    public FrmCitas() {
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagenes/Citas35.png")).getImage());
        this.getContentPane().setBackground(new java.awt.Color(244, 243, 176));
        initComponents();
        consultaCita = new ConsultaCita();
        buttonGroup1.add(optPresencial);
        buttonGroup1.add(optLlamada);
        buttonGroup1.add(optCorreo);
        optPresencial.setSelected(true);
        obtenerClientes();
        obtenerPromotores();
        explorarCitas();

        txtLugar.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (txtLugar.getText().length() == 45) {
                    e.consume();
                }
            }

            @Override
            public void keyPressed(KeyEvent arg0) {
            }

            @Override
            public void keyReleased(KeyEvent arg0) {
            }
        });
    }

    private void explorarCitas() {
        resultadoCitas = consultaCita.obtenerTodasLasCitas();
        numeroCitas = resultadoCitas.size();

        if (numeroCitas != 0) {

            indiceActual = 0;

            citaActual = resultadoCitas.get(indiceActual);
            idCita = citaActual.obtenerIdCita();
            cboCliente.setSelectedItem(citaActual.obtenerCliente());
            cboPromotor.setSelectedItem(citaActual.obtenerPromotor());
            txtFecha.setDate(citaActual.obtenerFecha());
            txtHora.setTime(citaActual.obtenerHora());
            txtLugar.setText(citaActual.obtenerLugar());
            txtObservaciones.setText(citaActual.obtenerObservaciones());
            establecerOpt(citaActual.obtenerTipo());

            obtenerIdCliente();
            obtenerIdPromotor();

            txtIndiceActual.setText("" + (indiceActual + 1));
            lblTotal.setText("" + numeroCitas);

        } else {
            cmdGuardar.setEnabled(false);
            cmdCancelar.setEnabled(false);
            cmdEliminar.setEnabled(false);
        }
    }

    private void obtenerClientes() {
        List<Cliente> resultadoClientes = (new ConsultaCliente()).obtenerTodosLosClientes();

        for (int x = 0; x < resultadoClientes.size(); x++) {
            cboCliente.addItem(resultadoClientes.get(x).obtenerNombre());
        }
    }

    private void obtenerPromotores() {
        List<Promotor> resultadoPromotores = (new ConsultaPromotor()).consultarPromotor();

        for (int x = 0; x < resultadoPromotores.size(); x++) {
            cboPromotor.addItem(resultadoPromotores.get(x).obtenerNombre());
        }
    }

    private void obtenerIdCliente() {
        clienteObtenido = cboCliente.getSelectedItem().toString();
        idCliente = (new ConsultaCliente()).obtenerIdCliente(clienteObtenido);
    }

    private void establecerOpt(String opcion) {
        switch (opcion) {
            case "Presencial":
                optPresencial.setSelected(true);
                break;
            case "Llamada":
                optLlamada.setSelected(true);
                break;
            case "Correo":
                optCorreo.setSelected(true);
                break;
        }
    }

    private void obtenerIdPromotor() {
        idPromotor = (new ConsultaPromotor()).obtenerIdPromotor(cboPromotor.getSelectedItem().toString());
    }

    private String optSeleccionado() {
        String resultado = null;

        if (optPresencial.isSelected()) {
            resultado = "Presencial";
        } else if (optLlamada.isSelected()) {
            resultado = "Llamada";
        } else if (optCorreo.isSelected()) {
            resultado = "Correo";
        }
        return resultado;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        lblCliente = new javax.swing.JLabel();
        cboCliente = new javax.swing.JComboBox();
        lblFecha = new javax.swing.JLabel();
        cmdBuscarCliente = new javax.swing.JButton();
        txtFecha = new com.toedter.calendar.JDateChooser();
        txtHora = new lu.tudor.santec.jtimechooser.JTimeChooser();
        lblHora = new javax.swing.JLabel();
        lblPromotor = new javax.swing.JLabel();
        cboPromotor = new javax.swing.JComboBox();
        lblLugar = new javax.swing.JLabel();
        txtLugar = new javax.swing.JTextField();
        cmdNuevo = new javax.swing.JButton();
        cmdGuardar = new javax.swing.JButton();
        cmdEliminar = new javax.swing.JButton();
        cmdCancelar = new javax.swing.JButton();
        cmdPrimero = new javax.swing.JButton();
        cmdAnterior = new javax.swing.JButton();
        txtIndiceActual = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        cmdSiguiente = new javax.swing.JButton();
        cmdUltimo = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        cmdBuscar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        optPresencial = new javax.swing.JRadioButton();
        optLlamada = new javax.swing.JRadioButton();
        optCorreo = new javax.swing.JRadioButton();
        lblObservaciones = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtObservaciones = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Citas");
        setBackground(new java.awt.Color(103, 196, 193));
        setResizable(false);

        lblCliente.setText("Cliente");

        cboCliente.setBackground(new java.awt.Color(255, 255, 255));

        lblFecha.setText("Fecha");

        cmdBuscarCliente.setBackground(java.awt.Color.white);
        cmdBuscarCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Busca.png"))); // NOI18N
        cmdBuscarCliente.setToolTipText("Buscar cliente");
        cmdBuscarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdBuscarClienteActionPerformed(evt);
            }
        });

        lblHora.setText("Hora");

        lblPromotor.setText("Promotor");

        cboPromotor.setBackground(new java.awt.Color(255, 255, 255));
        cboPromotor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboPromotorActionPerformed(evt);
            }
        });

        lblLugar.setText("Lugar");

        txtLugar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtLugarActionPerformed(evt);
            }
        });

        cmdNuevo.setBackground(java.awt.Color.white);
        cmdNuevo.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Nuevo.png"))); // NOI18N
        cmdNuevo.setMnemonic('n');
        cmdNuevo.setText("Nuevo");
        cmdNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNuevoActionPerformed(evt);
            }
        });

        cmdGuardar.setBackground(java.awt.Color.white);
        cmdGuardar.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Guardar.png"))); // NOI18N
        cmdGuardar.setMnemonic('g');
        cmdGuardar.setText("Guardar");
        cmdGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdGuardarActionPerformed(evt);
            }
        });

        cmdEliminar.setBackground(java.awt.Color.white);
        cmdEliminar.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Borrar.png"))); // NOI18N
        cmdEliminar.setMnemonic('e');
        cmdEliminar.setText("Eliminar");
        cmdEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEliminarActionPerformed(evt);
            }
        });

        cmdCancelar.setBackground(java.awt.Color.white);
        cmdCancelar.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Cancelar.png"))); // NOI18N
        cmdCancelar.setMnemonic('c');
        cmdCancelar.setText("Cancelar");
        cmdCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancelarActionPerformed(evt);
            }
        });

        cmdPrimero.setBackground(java.awt.Color.white);
        cmdPrimero.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/First.png"))); // NOI18N
        cmdPrimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdPrimeroActionPerformed(evt);
            }
        });

        cmdAnterior.setBackground(java.awt.Color.white);
        cmdAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Back.png"))); // NOI18N
        cmdAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdAnteriorActionPerformed(evt);
            }
        });

        txtIndiceActual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIndiceActualActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Ubuntu", 0, 18)); // NOI18N
        jLabel1.setText("/");

        lblTotal.setFont(new java.awt.Font("Ubuntu", 0, 24)); // NOI18N
        lblTotal.setText("100");

        cmdSiguiente.setBackground(java.awt.Color.white);
        cmdSiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Forward.png"))); // NOI18N
        cmdSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSiguienteActionPerformed(evt);
            }
        });

        cmdUltimo.setBackground(java.awt.Color.white);
        cmdUltimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Last.png"))); // NOI18N
        cmdUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdUltimoActionPerformed(evt);
            }
        });

        cmdBuscar.setBackground(java.awt.Color.white);
        cmdBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Search.png"))); // NOI18N
        cmdBuscar.setMnemonic('b');
        cmdBuscar.setText("Buscar cita");

        jPanel1.setBackground(new java.awt.Color(243, 181, 40));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Tipo"));
        jPanel1.setForeground(new java.awt.Color(243, 181, 40));

        optPresencial.setBackground(new java.awt.Color(243, 181, 40));
        optPresencial.setText("Presencial");

        optLlamada.setBackground(new java.awt.Color(243, 181, 40));
        optLlamada.setText("Llamada");

        optCorreo.setBackground(new java.awt.Color(243, 181, 40));
        optCorreo.setText("Correo");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(optCorreo)
                    .addComponent(optLlamada)
                    .addComponent(optPresencial))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(optPresencial)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(optLlamada)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(optCorreo)
                .addGap(0, 21, Short.MAX_VALUE))
        );

        lblObservaciones.setText("Observaciones");

        txtObservaciones.setColumns(20);
        txtObservaciones.setRows(5);
        jScrollPane1.setViewportView(txtObservaciones);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cmdNuevo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmdGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmdEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmdCancelar)
                        .addGap(18, 18, 18)
                        .addComponent(cmdBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(lblCliente)
                                .addComponent(lblPromotor)
                                .addComponent(lblLugar)
                                .addComponent(lblObservaciones))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(63, 63, 63)
                                .addComponent(lblFecha)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(32, 32, 32)
                                        .addComponent(lblHora)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(cboPromotor, javax.swing.GroupLayout.Alignment.LEADING, 0, 275, Short.MAX_VALUE)
                                            .addComponent(txtLugar, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(cboCliente, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cmdBuscarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(97, 97, 97))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
                                .addComponent(cmdPrimero)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdAnterior)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtIndiceActual, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel1)
                                .addGap(9, 9, 9)
                                .addComponent(lblTotal)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdSiguiente)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdUltimo)
                                .addGap(22, 22, 22))))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(cmdAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(cmdPrimero, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cmdUltimo, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(cmdSiguiente, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(lblTotal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel1)))))
                            .addComponent(txtIndiceActual, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cboCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblCliente))
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPromotor)
                            .addComponent(cboPromotor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblHora)
                            .addComponent(lblFecha))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblLugar)
                            .addComponent(txtLugar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblObservaciones)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(cmdBuscarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cmdEliminar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cmdGuardar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cmdCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cmdBuscar))
                    .addComponent(cmdNuevo))
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdBuscarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdBuscarClienteActionPerformed
        JDBuscaCliente ventanaBuscaCliente = new JDBuscaCliente(this, true, true);
        ventanaBuscaCliente.setLocationRelativeTo(this);
        ventanaBuscaCliente.setVisible(true);
        cboCliente.setSelectedItem(clienteObtenido);

    }//GEN-LAST:event_cmdBuscarClienteActionPerformed
    public static void establecerClienteCbo(String nombre) {
        clienteObtenido = nombre;
    }

    private void cboPromotorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboPromotorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cboPromotorActionPerformed

    private void txtLugarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtLugarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtLugarActionPerformed

    private void cmdGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdGuardarActionPerformed
        cmdGuardar.setEnabled(true);
        cmdCancelar.setEnabled(true);
        String mensaje = "";
        java.sql.Time horaSQL = null;
        int resultado = 0;
        obtenerIdCliente();
        obtenerIdPromotor();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
            horaSQL = new java.sql.Time(sdf.parse(txtHora.getFormatedTime()).getTime());
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        if (nuevaCita) {
            resultado = consultaCita.agregarNuevaCita(idCliente, idPromotor,
                    new java.sql.Date(txtFecha.getDate().getTime()), horaSQL,
                    txtLugar.getText(), txtObservaciones.getText(), optSeleccionado());
            mensaje = "Se programó nueva cita";
        } else {
            resultado = consultaCita.actualizarCita(idCliente, idPromotor,
                    new java.sql.Date(txtFecha.getDate().getTime()), horaSQL,
                    txtLugar.getText(), txtObservaciones.getText(), optSeleccionado(),
                    citaActual.obtenerIdCita());
            mensaje = "Se actualizó cita";
        }

        if (resultado == 1) {
            JOptionPane.showMessageDialog(null, mensaje, "Citas",
                    JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "No se pudo realizar la operación",
                    "Citas", JOptionPane.ERROR_MESSAGE);
        }

        nuevaCita = false;
        explorarCitas();

        cmdNuevo.setEnabled(true);
        cmdEliminar.setEnabled(true);
    }//GEN-LAST:event_cmdGuardarActionPerformed

    private void cmdNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNuevoActionPerformed
        nuevaCita = true;
        cboCliente.setSelectedIndex(0);
        cboPromotor.setSelectedIndex(0);
        txtFecha.setDate(null);
        txtLugar.setText("");
        txtObservaciones.setText("");
        cmdGuardar.setEnabled(true);
        cmdCancelar.setEnabled(true);
        cmdEliminar.setEnabled(false);
    }//GEN-LAST:event_cmdNuevoActionPerformed

    private void txtIndiceActualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIndiceActualActionPerformed
        String str = txtIndiceActual.getText();

        if (ValidaNumeros.validarNumeroEntero(str)) {

            indiceActual = (Integer.parseInt(str) - 1);

            if (numeroCitas != 0 && indiceActual < numeroCitas) {

                citaActual = resultadoCitas.get(indiceActual);
                idCita = citaActual.obtenerIdCita();
                cboCliente.setSelectedItem(citaActual.obtenerCliente());
                cboPromotor.setSelectedItem(citaActual.obtenerPromotor());
                txtFecha.setDate(citaActual.obtenerFecha());
                txtHora.setTime(citaActual.obtenerHora());
                txtLugar.setText(citaActual.obtenerLugar());
                txtObservaciones.setText(citaActual.obtenerObservaciones());
                establecerOpt(citaActual.obtenerTipo());
                obtenerIdCliente();
                obtenerIdPromotor();
            }
        } else {
            JOptionPane.showMessageDialog(null, "Ingresa un número válido",
                    "Promotores", JOptionPane.ERROR_MESSAGE);
            explorarCitas();
        }
    }//GEN-LAST:event_txtIndiceActualActionPerformed

    private void cmdPrimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdPrimeroActionPerformed
        indiceActual = 0;
        txtIndiceActual.setText("" + (indiceActual + 1));
        txtIndiceActualActionPerformed(evt);
    }//GEN-LAST:event_cmdPrimeroActionPerformed

    private void cmdAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAnteriorActionPerformed
        indiceActual--;

        if (indiceActual < 0) {
            indiceActual = numeroCitas - 1;
        }

        txtIndiceActual.setText("" + (indiceActual + 1));
        txtIndiceActualActionPerformed(evt);
    }//GEN-LAST:event_cmdAnteriorActionPerformed

    private void cmdSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSiguienteActionPerformed
        indiceActual++;

        if (indiceActual >= numeroCitas) {
            indiceActual = 0;
        }

        txtIndiceActual.setText("" + (indiceActual + 1));
        txtIndiceActualActionPerformed(evt);
    }//GEN-LAST:event_cmdSiguienteActionPerformed

    private void cmdUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdUltimoActionPerformed
        indiceActual = numeroCitas - 1;

        txtIndiceActual.setText("" + (indiceActual + 1));

        txtIndiceActualActionPerformed(evt);
    }//GEN-LAST:event_cmdUltimoActionPerformed

    private void cmdCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelarActionPerformed
        explorarCitas();
        nuevaCita = false;
        cmdNuevo.setEnabled(true);
        cmdGuardar.setEnabled(true);
        cmdCancelar.setEnabled(true);
        cmdEliminar.setEnabled(true);
    }//GEN-LAST:event_cmdCancelarActionPerformed

    private void cmdEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEliminarActionPerformed

        int opc = JOptionPane.showConfirmDialog(this, "¿Seguro que deseas "
                + "eliminar esta cita?",
                "Citas", JOptionPane.YES_NO_OPTION);

        int resultado = 0;

        if (opc == JOptionPane.YES_OPTION) {


            resultado = consultaCita.eliminarCita(citaActual.obtenerIdCita());

            if (resultado != 0) {

                JOptionPane.showMessageDialog(null, "Cita eliminada",
                        "Citas", JOptionPane.INFORMATION_MESSAGE);

                explorarCitas();

            } else {
                JOptionPane.showMessageDialog(null, "No se pudo eliminar cita",
                        "Citas", JOptionPane.INFORMATION_MESSAGE);
            }
        }

        cmdNuevo.setEnabled(true);
        cmdGuardar.setEnabled(true);
        cmdCancelar.setEnabled(true);
        cmdEliminar.setEnabled(true);
    }//GEN-LAST:event_cmdEliminarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cboCliente;
    private javax.swing.JComboBox cboPromotor;
    private javax.swing.JButton cmdAnterior;
    private javax.swing.JButton cmdBuscar;
    private javax.swing.JButton cmdBuscarCliente;
    private javax.swing.JButton cmdCancelar;
    private javax.swing.JButton cmdEliminar;
    private javax.swing.JButton cmdGuardar;
    private javax.swing.JButton cmdNuevo;
    private javax.swing.JButton cmdPrimero;
    private javax.swing.JButton cmdSiguiente;
    private javax.swing.JButton cmdUltimo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblCliente;
    private javax.swing.JLabel lblFecha;
    private javax.swing.JLabel lblHora;
    private javax.swing.JLabel lblLugar;
    private javax.swing.JLabel lblObservaciones;
    private javax.swing.JLabel lblPromotor;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JRadioButton optCorreo;
    private javax.swing.JRadioButton optLlamada;
    private javax.swing.JRadioButton optPresencial;
    private com.toedter.calendar.JDateChooser txtFecha;
    private lu.tudor.santec.jtimechooser.JTimeChooser txtHora;
    private javax.swing.JTextField txtIndiceActual;
    private javax.swing.JTextField txtLugar;
    private javax.swing.JTextArea txtObservaciones;
    // End of variables declaration//GEN-END:variables
}
